/*****
author : sailing
date: 2016/11/20
******/
import Config from './config'
import tools from './tools'
import dataC from './data'
export default {
    resetLineStyle(cy) {
        dataC.cy.edges(function(i, edge) {
            edge.style(tools.resetLine())
        })
    },
    setPort(cy) {
        var setFlag = false
        for (var i = 0; i < dataC.nodes.length; i++) {
            var node = dataC.nodes[i]
            if (node.data['type'] === 'distributing_cabinet') {
                console.log('ndoe.data', node.data['type'])
                var id = node.data['id'].toString()
                var lineObj = this.find(id)
                this.comparison(lineObj, id, setFlag)
            }
        }
        //alert("dfee")
        Config.topVue.$message('没有异常端口');
        if (!setFlag) Config.topVue.$message('没有异常端口');
    },
    setColor(id) {
        dataC.cy.nodes(function(i, node) {
            var data = node.data()
            if (id === data['id']) {
                node.addClass('eater')
            }
        })
    },
    comparison(obj, id, setFlag) {
        if (obj.lineS.length !== obj.lineE.length) {
            this.setColor(id)
            return
        }

        for (var i = 0; i < obj.lineS.length; i++) {
            var port = obj.lineS[i]
            var flag = false
            for (var j = 0; j < obj.lineE.length; j++) {
                if (port === obj.lineE[j]) {
                    flag = true
                    break
                }
            }

            if (!flag) {
                this.setColor(id)
                setFlag = true
                return
            }
        }
    },
    find(id) {
        var lineObj = {
            lineS: [],
            lineE: []
        }
        if (dataC.edges) {
            for (var i = 0; i < dataC.edges.length; i++) {
                var line = dataC.edges[i]
                var source = line['data']['source'].toString()
                var target = line['data']['target'].toString()
                var sPort = line['data']['s_port']
                var ePort = line['data']['e_port']
                if (source === id.toString()) {
                    lineObj.lineS.push(sPort)
                }

                if (target === id.toString()) {
                    lineObj.lineE.push(ePort)
                }
            }
        }

        return lineObj
    }
}